package org.jeecgframework.web.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.web.service.UploadEngineerService;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.ZyEngineerEntity;
import org.jeecgframework.web.util.ExcelUtil;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**   
 * @Title: UploadContactsServiceImpl
 * @Description: 上传联系人信息
 * @author ZhangYunlong
 * @date 2016-04-01
 * @version V1.0   
 *
 */
@Service("uploadEngineerService")
@Transactional
public class UploadEngineerServiceImpl extends CommonServiceImpl implements
		UploadEngineerService {

	/**
	 * @Description 解析EXCEL
	 * @param HttpServletRequest 通过前台请求获得文件流
	 */
	public List<ZyEngineerEntity> parseExcel(HttpServletRequest req) {
		List<ZyEngineerEntity> excelList = new ArrayList<ZyEngineerEntity>();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		XSSFWorkbook hwk = null;
		try {
			for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
				MultipartFile file = entity.getValue();// 获取上传文件对象
				hwk = ExcelUtil.analyzeExcel2(file.getInputStream());
				excelList = parseExcelToList(hwk);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return excelList;
	}

	/**
	 * @Description 将通过POI解析出来的EXCEL转换成对应的实体LIST
	 * @param XSSFWorkbook Excel2007+对象
	 */
	private List<ZyEngineerEntity> parseExcelToList(XSSFWorkbook hwb) {

		List<ZyEngineerEntity> result = new ArrayList<ZyEngineerEntity>();
		// 得到第一个工作表
		XSSFSheet sheet = hwb.getSheetAt(0);
		XSSFRow row = null;
		ZyEngineerEntity zyEngineerEntity;
//			List<ZyEngineerEntity> parentEngineer;
		TSDepart belongOrg = null;
		for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
			row = sheet.getRow(j);
			zyEngineerEntity = new ZyEngineerEntity();
			zyEngineerEntity.setIdNum(ExcelUtil.getCellValue(row.getCell(0)));
			zyEngineerEntity.setName(ExcelUtil.getCellValue(row.getCell(1)));
			belongOrg = findUniqueByProperty(TSDepart.class, "shortname", ExcelUtil.getCellValue(row.getCell(2)));
			zyEngineerEntity.setBelongOrg(belongOrg);
			zyEngineerEntity.setCellphone(ExcelUtil.getCellValue(row.getCell(3)));
			zyEngineerEntity.setGender(ExcelUtil.getCellValue(row.getCell(4)).equals("男")?true:false);
			zyEngineerEntity.setAuthentication(ExcelUtil.getCellValue(row.getCell(5)));
			zyEngineerEntity.setCertificateNum(ExcelUtil.getCellValue(row.getCell(6)));
			/*parentEngineer = findByProperty(ZyEngineerEntity.class, "name", ExcelUtil.getCellValue(row.getCell(7)));
			if(parentEngineer.size()>0){
				zyEngineerEntity.setParentId(parentEngineer.get(0));
			}*/
			zyEngineerEntity.setParentId(getPath(ExcelUtil.getCellValue(row.getCell(7))));
			zyEngineerEntity.setIsAreaEngineer(ExcelUtil.getCellValue(row.getCell(8)).equals("是")?true:false);
			zyEngineerEntity.setState(ExcelUtil.getCellValue(row.getCell(9)).equals("在职")?StaticValue.DIC_ENGINEER_STATE_ONJOB:StaticValue.DIC_ENGINEER_STATE_QUIT);
			zyEngineerEntity.setEmail(ExcelUtil.getCellValue(row.getCell(10)));
			zyEngineerEntity.setEngineerNo(ExcelUtil.getCellValue(row.getCell(11)));
			zyEngineerEntity.setIsDelete("0");
			result.add(zyEngineerEntity);
		}
		return result;
	}
	
	private ZyEngineerEntity getPath(String name){
		ZyEngineerEntity parentEngineer = null;
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM zy_engineer t where ISNULL(t.parent_id) ").
			append("and t.name = '").
			append(name).
			append("'");
		Map<String, Object> filePathMap = this.findOneForJdbc(sql.toString());
		if(null != filePathMap&& null != filePathMap.get("id")){
			parentEngineer = getEntity(ZyEngineerEntity.class,filePathMap.get("id").toString());
		}
		return parentEngineer;
	}

	/**
	 * @Description 批量保存
	 * @param List<T>
	 */
	public <T> void batchSave(List<T> entitys) {
		for (T t : entitys) {
			super.save(t);
		}
	}
}
